package com.collaboration.proton.controllers;

import com.collaboration.proton.dao.PlayerRepository;
import com.collaboration.proton.entities.GameZone;
import com.collaboration.proton.entities.Player;
import com.collaboration.proton.entities.PlayerInformation;
import com.collaboration.proton.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author vybfi
 * @param
 * @return 无
 * 这个类是用户表的控制层
 */
@Controller
@RequestMapping("")
public class SecurityController {

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    SingleAgencyInfoService singleAgencyInfoService;

    @Autowired
    HeroService heroService;

    @Autowired
    GameZoneService gameZoneService ;

    @Autowired
    PlayerInformationService playerInformationService;

    @Autowired
    PlayerService playerService;

    @Autowired
    UserHeroService userHeroService;


    @GetMapping("register")
    public String displayRegister(Model model){
        Player player = new Player();
        model.addAttribute( "player", player);
        return "security/register";
    }

    @GetMapping("login")
    public String login(){
        return "security/login";
    }

    @GetMapping("logout")
    public String logout(){
        return "security/logout";
    }

    @PostMapping("register/save")
    public String saveUser(Model model, Player player){
        player.setPassword(bCryptPasswordEncoder.encode(player.getPassword()));
        player.setRole("ROLE_USER");
        playerService.save(player);

        PlayerInformation playerInformation= new PlayerInformation(0,0,player);
        playerInformationService.save(playerInformation);

//        GameZone gameZone=new GameZone("大厅");
//        gameZone.getPlayers().add(player);
//        gameZoneService.save(gameZone);

        return  "redirect:/login";
    }

    @GetMapping
    public String def(){
        return "redirect:/home";
    }

}
